package com.krautzlabs;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

public class MapReader extends DefaultHandler{
	private GameState gameState;
	public MapReader(GameState gameState)
	{
		this.gameState = gameState;
	}
	
	public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException
	{
		if(localName.equals("mappoint"))
		{
			MapPoint mp;
			
			if(attributes.getValue("side") != null)
			{
				Player player;
				Player.PlayerSide playerSide;
				if(attributes.getValue("side").equals("AXIS"))
					 playerSide = Player.PlayerSide.AXIS;
				else
					playerSide = Player.PlayerSide.ALLIED;
					
				player = new Player("player"+attributes.getValue("side"),playerSide);
				
				mp = new BasePoint(Integer.parseInt(attributes.getValue("x")), Integer.parseInt( attributes.getValue("y")), Integer.parseInt( attributes.getValue("id")), player);
					
			}
			else
			{
				mp = new CapturePoint(Integer.parseInt(attributes.getValue("x")), Integer.parseInt(attributes.getValue("y")), Integer.parseInt( attributes.getValue("id")));
			}
			
			this.gameState.addMapPoint(mp);
		}
		else if(localName.equals("connected"))
		{
			this.gameState.addMapPointConnection(Integer.parseInt(attributes.getValue("point1")), Integer.parseInt(attributes.getValue("point2")));
		}
	}
	
	public void endElement(String uri, String localName, String qName) throws SAXException {
	
	}
	
	public void characters(char[] ch, int start, int length) throws SAXException {
		
	}
}
